{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f57ae0",
   "metadata": {},
   "source": [
    "# 1 百度AI开放平台-OCR（光学字符识别）\n",
    "\n",
    "# 学生实践周\n",
    "\n",
    "## 1.1通用文字识别\n",
    "\n",
    "* 接口描述\n",
    "> 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。\n",
    "\n",
    "* 请求示例\n",
    "> 1.HTTP 方法 ：POST\n",
    "\n",
    "> 1.请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "\n",
    "* 请求参数\n",
    "> 1.\n",
    "> "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a6a55ea9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'8Og1dSh61Kb9ckSoKo22KwEA',\n",
    "    'client_secret':'bZXkcki4wpRI8gtFMbiAA3Zb8dI8xr5e'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())\n",
    "    access_token = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5a8d5d8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.e3a482a7d9fc97b909e76949413640b0.2592000.1652357877.282335-25869149'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1bbc4e0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 6, 'error_msg': 'No permission to access data'}\n"
     ]
    }
   ],
   "source": [
    "## 实践A-1\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('gznfxy.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = \"24.e3a482a7d9fc97b909e76949413640b0.2592000.1652357877.282335-25869149\"\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c6f06d0",
   "metadata": {},
   "source": [
    "## 2.2网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4776cbef",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "85908418",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 6, 'error_msg': 'No permission to access data'}\n"
     ]
    }
   ],
   "source": [
    "## 实践A-1\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('gznfxy.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.e3a482a7d9fc97b909e76949413640b0.2592000.1652357877.282335-25869149'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1275d415",
   "metadata": {},
   "source": [
    "## 2.3 手写文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a0ef989b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "296f4987",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "047f7eb6",
   "metadata": {},
   "source": [
    "## 2.4 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c1fb0646",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f18b13",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "311746d6",
   "metadata": {},
   "source": [
    "## 2.5 卡片类文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "704a97f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 实践-5  身份证 &  银行卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a81140b8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ca9fb7e4",
   "metadata": {},
   "source": [
    "## 2.6财务类文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17f5e81e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
